package de.idgard.android.addon.ui.services.impl;

import android.content.Context;
import de.idgard.android.addon.ui.services.AddonStateService;
import de.idgard.android.addon.ui.services.BrowserPrivateModeService;
import de.idgard.android.addon.ui.services.BrowserTabService;
import de.idgard.android.addon.ui.services.BrowserUtilityService;
import de.idgard.android.addon.ui.services.JavaScriptLoaderService;
import de.idgard.android.addon.ui.services.LayoutAdjustmentService;
import de.idgard.android.addon.ui.services.NewsService;
import de.idgard.android.addon.ui.services.OrchestrationService;
import de.idgard.android.addon.ui.services.PreferenceService;
import de.idgard.android.addon.ui.services.PropertiesService;
import de.idgard.android.addon.ui.services.TransparencyStateService;
import de.idgard.android.addon.ui.services.UpdateService;
import de.idgard.android.addon.ui.services.WebIconService;
import de.idgard.android.addon.ui.services.ZircoToIDGardAPI;

/**
 * 
 * @author ckaratza
 * 
 */

public final class UIServiceRegistry {

	private static UIServiceRegistry instance;

	private UIServiceRegistry() {

	}

	public void resetContext(Context ctx) {
		((PropertiesServiceImpl) propertiesService).setContext(ctx);
		((PreferenceServiceImpl) preferenceService).setContext(ctx);

		((LayoutAdjustmentServiceImpl) layoutAdjustmentService).setContext(ctx);

		((JavaScriptLoaderImpl) javaScriptLoaderService).setContext(ctx);
		((ZircoToIDGardAPIImpl) zircoToIDGardAPI).setContext(ctx);
	}

	public static UIServiceRegistry instance() {
		if (instance == null) {
			instance = new UIServiceRegistry();
		}
		return instance;
	}

	private ZircoToIDGardAPI zircoToIDGardAPI;

	private OrchestrationService orchestrationService;
	private PropertiesService propertiesService;
	private AddonStateService addonStateService;
	private BrowserUtilityService browserUtilityService;
	private BrowserPrivateModeService browserPrivateModeService;
	private BrowserTabService browserTabService;
	private PreferenceService preferenceService;
	private TransparencyStateService transparencyStateService;
	private JavaScriptLoaderService javaScriptLoaderService;
	private UpdateService updateService;

	private WebIconService webiconservice;
	private NewsService newsservice;
	private LayoutAdjustmentService layoutAdjustmentService;
	private boolean init = false;

	
	
	public UpdateService getUpdateService() {
		return updateService;
	}

	public void init(Context ctx) {
		if (init) {
			return;
		}
		zircoToIDGardAPI = new ZircoToIDGardAPIImpl();
		((ZircoToIDGardAPIImpl) zircoToIDGardAPI).setContext(ctx);
		propertiesService = new PropertiesServiceImpl();
		((PropertiesServiceImpl) propertiesService).setContext(ctx);
		preferenceService = new PreferenceServiceImpl();
		((PreferenceServiceImpl) preferenceService).setContext(ctx);

		addonStateService = new AddonStateServiceImpl();
		transparencyStateService = new TransparencyStateServiceImpl();

		browserUtilityService = new BrowserUtilityServiceImpl();
		((BrowserUtilityServiceImpl) browserUtilityService)
				.setPropertiesService(propertiesService);
		((BrowserUtilityServiceImpl) browserUtilityService)
				.setAddonStateService(addonStateService);
		((BrowserUtilityServiceImpl) browserUtilityService)
				.setZircoToIDGardAPI(zircoToIDGardAPI);

		browserPrivateModeService = new BrowserPrivateModeServiceImpl();
		((BrowserPrivateModeServiceImpl) browserPrivateModeService)
				.setAddonStateService(addonStateService);
		((BrowserPrivateModeServiceImpl) browserPrivateModeService)
				.setTransparencyStateService(transparencyStateService);
		((BrowserPrivateModeServiceImpl) browserPrivateModeService)
				.setZircoToIDGardAPI(zircoToIDGardAPI);

		browserTabService = new BrowserTabServiceImpl();
		((BrowserTabServiceImpl) browserTabService)
				.setBrowserUtilityService(browserUtilityService);
		((BrowserTabServiceImpl) browserTabService)
				.setZircoToIDGardAPI(zircoToIDGardAPI);

		webiconservice = new WebIconServiceImpl();
		((WebIconServiceImpl) webiconservice)
				.setZircoToIDGardAPI(zircoToIDGardAPI);
		newsservice = new NewsServiceImpl();

		layoutAdjustmentService = new LayoutAdjustmentServiceImpl();
		((LayoutAdjustmentServiceImpl) layoutAdjustmentService).setContext(ctx);

		orchestrationService = new OrchestrationServiceImpl();
		((OrchestrationServiceImpl) orchestrationService)
				.setAddonStateService(addonStateService);
		((OrchestrationServiceImpl) orchestrationService)
				.setBrowserUtilityService(browserUtilityService);
		((OrchestrationServiceImpl) orchestrationService)
				.setBrowserTabService(browserTabService);
		((OrchestrationServiceImpl) orchestrationService)
				.setBrowserPrivateModeService(browserPrivateModeService);
		((OrchestrationServiceImpl) orchestrationService)
				.setTransparencyStateService(transparencyStateService);
		((OrchestrationServiceImpl) orchestrationService)
				.setApi(zircoToIDGardAPI);
		javaScriptLoaderService = new JavaScriptLoaderImpl();
		((JavaScriptLoaderImpl) javaScriptLoaderService).setContext(ctx);

		updateService = new UpdateServiceImpl();
		((UpdateServiceImpl) updateService)
				.setPropertiesService(propertiesService);

		init = true;
	}

	public ZircoToIDGardAPI getZircoToIDGardAPI() {
		return zircoToIDGardAPI;
	}

	public OrchestrationService getOrchestrationService() {
		return orchestrationService;
	}

	public BrowserTabService getBrowserTabService() {
		return browserTabService;
	}

	public PropertiesService getPropertiesService() {
		return propertiesService;
	}

	public AddonStateService getAddonStateService() {
		return addonStateService;
	}

	public BrowserUtilityService getBrowserUtilityService() {
		return browserUtilityService;
	}

	public BrowserPrivateModeService getBrowserPrivateModeService() {
		return browserPrivateModeService;
	}

	public PreferenceService getPreferenceService() {
		return preferenceService;
	}

	public TransparencyStateService getTransparencyStateService() {
		return transparencyStateService;
	}

	public WebIconService getWebIconService() {
		return webiconservice;
	}

	public NewsService getNewsService() {
		return newsservice;
	}

	public LayoutAdjustmentService getlayouAdjustmentService() {
		return layoutAdjustmentService;
	}

	public JavaScriptLoaderService getJavaScriptLoaderService() {
		return javaScriptLoaderService;
	}
}
